
## This makes a CSV file with the matches so it can be
## easily merged into form letters to send out to 
## participants

MT = read.csv( "final_matches_done.csv", stringsAsFactors=FALSE )
MT[ MT==" " ] = ""
MT[is.na(MT)] = ""

## Format of MT is:
## personID pubname email comments Rnd1, Rnd2, Rnd3, Rnd4 ..., Cruise1, Cruise2, Cruise3, ...

## general idea for dates--
## make a 2d matrix of boolean matches.  
## Do this by cruising through the list and marking true for any id 
## that is in the row of the owner id.
## Do a transposition and a 'and,' 
## Look for remaining mutual matches.  Then prettify.

## general idea for cruises--make sure the cruised people get the email addrs of
## who cruised them.

## WARNING: some limits for number of matches and cruises received are hardcoded.
##    data structures will probably stretch to work, as needed.

rndCols = grep( "Rnd", names(MT) )
cruiseCols = grep( "Cruise", names(MT) )

for( i in c(rndCols,cruiseCols) ) {
	MT[[i]] = toupper(MT[[i]])
}

N = nrow(MT)

##########################################################################################
###                  Compute the actual dating matches 
##########################################################################################
mat = matrix( rep( FALSE, N^2 ), nrow=N )

## For each row, mark off any ids that are left in the matrix
## These are the people that row wanted.
for ( i in 1:N ) {
	mat[i,] = MT$personID %in% MT[i, rndCols] 
}

m2 = mat & t(mat)
m2 = as.data.frame( m2 )
rownames(m2) = MT$personID
colnames(m2) = MT$personID


# Utility function that assembles information into an appropriate list.
makeNameList = function(nms, rnms, email, cols, nope.text= "Sorry!  No matches this time.") {
	if ( cols==1 ) {
		folks = paste( rnms, " (", nms, ") ", email, sep="", collapse=";" )
	} else {
		folks = paste( rnms, " (", nms, ") ", email, sep="" )
	}		
	if ( folks[1] == " () " ) {
		folks = nope.text
	}
	flist = rep( "", cols )
	flist[ 1:length(folks) ] = folks
	flist
}



#COLS = max( apply(m2, 1, sum ) )   # matches seperated
COLS=1                             # matches packed into string
allL = matrix( nrow=N, ncol=COLS )
colnames(allL) = paste( "Date", 1:COLS, sep="" )

for ( i in 1:N ) {

	r = m2[[i]]  # pull out matches as boolean vector
	#print( r )
	# now grab id, name, and email of all TRUE matches.
	nms = MT$personID[r]
	rnms = MT$pubname[r]
	email = MT$email[r]
	
	# pack into our matrix of matches.  Empty spots will be ""
	allL[ i, ] = makeNameList( nms, rnms, email, cols=COLS )
}

allL = as.data.frame(allL)
rownames(allL) = MT$personID

allL = cbind( personID= MT$personID, email=MT$email, pubname=MT$pubname, comments=MT$comments, allL )
allL$num.matches = apply(m2, 1, sum )
allL$num.desired = apply( mat, 1, sum )
allL$num.possible = apply( mat, 2, sum )

cat( "* Matching complete\n" )

### The CRUISES are next...
#COLS = 3  # This is a GUESS as to the most cruises someone would get.
COLS = 1
allC = matrix( nrow=N, ncol=COLS )
colnames(allC) = paste( "Cruise", 1:COLS, sep="" )

# make cruises
num.cruises = rep(0,N)
for ( i in 1:N ) {
	ID = MT$personID[i]
	# did anyone cruise ID?  If so, get who they are.
	gotF = MT$Cruise1 == ID | MT$Cruise2 == ID | MT$Cruise3 == ID
	ids = MT$personID[gotF]
	emails = MT$email[gotF]
	names = MT$pubname[gotF]
	num.cruises[i] = length(ids)
	allC[i,] = makeNameList( ids, names, emails, nope.text="No cruises.", cols=COLS )
}

allC$cruised = do.call( paste, c( MT[ cruiseCols ], sep=" " ) )

cat( "* Cruise calculations complete.\n" )


allL = cbind( allL, allC )
allL$num.cruises= num.cruises


write.csv( allL, file="finals.csv", quote=FALSE )

cat( "* Final results written to finals.csv.\n" )
